// pages/index/index.js
var a = getApp();
var util = require('../../utils/util.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;

let statusBarHeight = wx.getSystemInfoSync().statusBarHeight
const week = util.week()
const tPart = getPart()

var base = require('../../utils/lang.js');
const _ = base._
// _t: base._t(),

// 获取天天特价场次
// 1(早0-10)，2（上10.01-14.00），3（下午14.01-18.00），4（晚场18.01-23.59）
function getPart() {
	let _part = 1
	let _hour = new Date().getHours()
	if (_hour >= 0 && _hour < 10) {
		_part = 1
	} else if (_hour >= 10 && _hour < 14) {
		_part = 2
	} else if (_hour >= 14 && _hour < 18) {
		_part = 3
	} else if (_hour >= 18 && _hour < 24) {
		_part = 4
	}
	return _part
}

function countDown() {
	let end = 0
	let _h = new Date().getHours()
	let _m = new Date().getMinutes()
	let _s = new Date().getSeconds()

	if (_h >= 0 && _h < 10) {
		end = 10
	} else if (_h >= 10 && _h < 14) {
		end = 14
	} else if (_h >= 14 && _h < 18) {
		end = 18
	} else if (_h >= 18 && _h < 24) {
		end = 24
	}

	const h = end - _h - 1
	const m = 59 - _m
	const s = 60 - _s
	return {
		h,
		m,
		s
	}
}

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		_t: base._t(),
		language: '中文',
		brand2Array: [],
		mdxx: {},
		tTimer: null,
		tCountDown: {
			h: 0,
			m: 0,
			s: 0
		},
		tSaleWeek: week, // 天天特价星期
		weekArr: [_('星期一'), _('星期二'), _('星期三'), _('星期四'), _('星期五'), _('星期六'), _('星期日')],
		tPart: tPart, // 天天特价场次
		tParts: ['00:00', '10:00', '14:00', '18:00'],
		tPagesize: 6, // 天天特价6条数据
		tDataLeft: [],
		tDataRight: [],
		statusBarHeight: statusBarHeight * 2,
		maskTop: statusBarHeight * 2 + 100,
		mLogo: '',
		mName: '',
		colorOne: '',
		colorTwo: '',
		mScrollAdArr: [],
		mCouponTitleArr: [_('外卖券'), _('堂食券'), _('通用券')],
		mCouponIndex: 0,
		mCouponList: [],
		mCouponShowStart: 0,
		mCouponShowEnd: 2,
		mCouponShow: [],
		mYouxuanList: [],
		fangyiData: '',
		characteristicList: [{
			text: _('0配送费')
		}, {
			text: _("0元起送")
		}],
		sortList: [{
			sort: _("综合排序"),
			image: "",
		}, {
			sort: _("销量最高"),
			image: "",
		}, {
			sort: _("起送价最低"),
			image: "",
		}, {
			sort: _("配送费最低"),
			image: "",
		}],
		discountList: [{
			icon: _("减"),
			iconColor: "#FF635B",
			text: _("满减优惠"),
			zdname: ' and d.money is not null'
		}, {
			icon: _("新"),
			iconColor: "#34aaff",
			text: _("新用户立减"),
			zdname: ' and c.xyh_open=1'
		}, {
			icon: _("提"),
			iconColor: "#6FDF64",
			text: _("到店自提"),
			zdname: ' and c.is_zt=1'
		}],
		categoryList: {
			pageone: [{
				name: "美食",
				src: "/pages/images/1.png"
			}, {
				name: "甜点饮品",
				src: "/pages/images/2.png"
			}, {
				name: "美团超市",
				src: "/pages/images/3.png"
			}, {
				name: "正餐精选",
				src: "/pages/images/4.png"
			}, {
				name: "生鲜果蔬",
				src: "/pages/images/5.png"
			}, {
				name: "全部商家",
				src: "/pages/images/6.png"
			}, {
				name: "免配送费",
				src: "/pages/images/7.png"
			}, {
				name: "新商家",
				src: "/pages/images/8.png"
			}],
			pagetwo: [{
				name: "美食",
				src: "/pages/images/1.png"
			}, {
				name: "甜点饮品",
				src: "/pages/images/2.png"
			}, {
				name: "美团超市",
				src: "/pages/images/3.png"
			}, {
				name: "正餐精选",
				src: "/pages/images/4.png"
			}, {
				name: "生鲜果蔬",
				src: "/pages/images/5.png"
			}, {
				name: "全部商家",
				src: "/pages/images/6.png"
			}, {
				name: "免配送费",
				src: "/pages/images/7.png"
			}, {
				name: "新商家",
				src: "/pages/images/8.png"
			}]
		},
		params: {
			nopsf: 2,
			nostart: 2,
			yhhd: ''
		},
		issx: false,
		selected: 0,
		mask1Hidden: true,
		mask2Hidden: true,
		animationData: "",
		location: "",
		characteristicSelected: [false, false],
		discountSelected: null,
		selectedNumb: 0,
		sortSelected: _("综合排序"),
		pagenum: 1,
		storelist: [],
		bfstorelist: [],
		mygd: false,
		jzgd: true,
		isjzz: true,
	},
	mScanFn() {
		wx.scanCode({
			success() {}
		})
	},
	//tjhb
	refresh: function() {
		var that = this
		// 红包1
		var animationData_4 = wx.createAnimation({
			duration: 3000,
			timingFunction: 'linear',
		})
		animationData_4.opacity(0.2).step({
				duration: 200
			})
			.opacity(0.3).scale(1.1, 1.1).translate3d(0, 10, 0).step({
				duration: 200
			})
			.opacity(0.4).scale(1.2, 1.2).translate3d(0, 30, 0).step({
				duration: 200
			})
			.opacity(0.5).scale(1.3, 1.3).translate3d(0, 50, 0).step({
				duration: 200
			})
			.opacity(0.6).scale(1.4, 1.4).translate3d(0, 70, 0).step({
				duration: 200
			})
			.opacity(0.7).translate3d(0, 90, 0).step({
				duration: 200
			})
			.opacity(0.8).translate3d(0, 110, 0).step({
				duration: 200
			})
			.opacity(0.9).translate3d(0, 130, 0).step({
				duration: 200
			})
			.opacity(1).translate3d(0, 140, 0).step({
				duration: 200
			})
		that.setData({
			animationData_4: animationData_4.export()
		})

		// 红包2
		var animationData_5 = wx.createAnimation({
			duration: 3000,
			timingFunction: 'linear',
		})
		animationData_5.opacity(0.2).step({
				duration: 200
			})
			.opacity(0.3).scale(1.1, 1.1).translate3d(-10, 10, 0).step({
				duration: 200
			})
			.opacity(0.4).scale(1.2, 1.2).translate3d(-15, 30, 0).step({
				duration: 200
			})
			.opacity(0.5).scale(1.3, 1.3).translate3d(-20, 50, 0).step({
				duration: 200
			})
			.opacity(0.6).scale(1.4, 1.4).translate3d(-25, 70, 0).step({
				duration: 200
			})
			.opacity(0.7).translate3d(-30, 90, 0).step({
				duration: 200
			})
			.opacity(0.8).translate3d(-35, 110, 0).step({
				duration: 200
			})
			.opacity(0.9).translate3d(-40, 130, 0).step({
				duration: 200
			})
			.opacity(1).translate3d(-30, 150, 0).step({
				duration: 200
			})
		that.setData({
			animationData_5: animationData_5.export()
		})
		// 天降红包字样
		var animation = wx.createAnimation({
			duration: 3000,
			timingFunction: 'linear',
		})
		animation.translate3d(0, 0, 0).scale(0, 0).translate3d(0, 0, 0).step({
				duration: 200
			})
			.scale(0.2, 0.2).translate3d(0, 20, 0).step({
				duration: 200
			})
			.scale(0.4, 0.4).translate3d(0, 40, 0).step({
				duration: 200
			})
			.scale(0.6, 0.6).translate3d(0, 60, 0).step({
				duration: 200
			})
			.scale(0.8, 0.8).translate3d(0, 80, 0).step({
				duration: 200
			})
			.scale(1, 1).translate3d(0, 90, 0).step({
				duration: 200
			})
		that.setData({
			animationData: animation.export()
		})
		// 主体背景
		var animationData_1 = wx.createAnimation({
			duration: 3000,
			timingFunction: 'linear',
		})
		animationData_1.scale(0, 0).step({
				duration: 100
			})
			.scale(0.2, 0.2).step({
				duration: 100
			})
			.scale(0.4, 0.4).step({
				duration: 100
			})
			.scale(0.6, 0.6).step({
				duration: 200
			})
			.scale(0.8, 0.8).step({
				duration: 200
			})
			.scale(1, 1).step({
				duration: 200
			})
		that.setData({
			animationData_1: animationData_1.export()
		})
		// 第一个金币
		setTimeout(function() {
			console.log('开始执行')
			var animationData_2 = wx.createAnimation({
				duration: 3000,
				timingFunction: 'linear',
			})
			animationData_2.opacity(0.1).step({
					duration: 100
				})
				.opacity(0.3).scale(1.1, 1.1).translate3d(10, 10, 0).step({
					duration: 100
				})
				.opacity(0.4).scale(1.2, 1.2).translate3d(11, 15, 0).step({
					duration: 100
				})
				.opacity(0.5).scale(1.3, 1.3).translate3d(12, 20, 0).step({
					duration: 100
				})
				.opacity(0.6).scale(1.4, 1.4).translate3d(13, 25, 0).step({
					duration: 100
				})
				.opacity(0.7).translate3d(14, 30, 0).step({
					duration: 100
				})
				.opacity(0.8).translate3d(15, 35, 0).step({
					duration: 100
				})
				.opacity(0.9).translate3d(16, 40, 0).step({
					duration: 100
				})
				.opacity(1).translate3d(17, 45, 0).step({
					duration: 100
				})
			that.setData({
				animationData_2: animationData_2.export()
			})
		}, 700)
		// 第三个金币
		setTimeout(function() {
			var animationData_6 = wx.createAnimation({
				duration: 3000,
				timingFunction: 'linear',
			})
			animationData_6.opacity(0.1).step({
					duration: 100
				})
				.opacity(0.3).scale(1.1, 1.1).translate3d(-100, 10, 0).step({
					duration: 70
				})
				.opacity(0.4).scale(1.2, 1.2).translate3d(-110, 15, 0).step({
					duration: 70
				})
				.opacity(0.5).scale(1.3, 1.3).translate3d(-120, 20, 0).step({
					duration: 70
				})
				.opacity(0.6).scale(1.4, 1.4).translate3d(-130, 25, 0).step({
					duration: 100
				})
				.opacity(0.7).translate3d(-120, 30, 0).step({
					duration: 130
				})
				.opacity(0.8).translate3d(-110, 35, 0).step({
					duration: 130
				})
				.opacity(0.9).translate3d(-100, 40, 0).step({
					duration: 130
				})
				.opacity(1).translate3d(-90, 45, 0).step({
					duration: 130
				})
			that.setData({
				animationData_6: animationData_6.export()
			})
		}, 700)
		// 第四个金币
		setTimeout(function() {
			var animationData_7 = wx.createAnimation({
				duration: 3000,
				timingFunction: 'linear',
			})
			animationData_7.opacity(0.1).translate3d(0, 0, 0).step({
					duration: 100
				})
				.opacity(0.3).scale(1.1, 1.1).translate3d(10, 10, 0).step({
					duration: 70
				})
				.opacity(0.4).scale(1.2, 1.2).translate3d(20, 15, 0).step({
					duration: 70
				})
				.opacity(0.5).scale(1.3, 1.3).translate3d(30, 20, 0).step({
					duration: 70
				})
				.opacity(0.6).scale(1.4, 1.4).translate3d(40, 25, 0).step({
					duration: 100
				})
				.opacity(0.7).translate3d(50, 30, 0).step({
					duration: 200
				})
				.opacity(0.8).translate3d(60, 35, 0).step({
					duration: 200
				})
				.opacity(0.9).translate3d(70, 40, 0).step({
					duration: 200
				})
				.opacity(1).translate3d(80, 45, 0).step({
					duration: 300
				})
			that.setData({
				animationData_7: animationData_7.export()
			})
		}, 700)
		// 树叶
		setTimeout(function() {
			var animationData_8 = wx.createAnimation({
				duration: 3000,
				timingFunction: 'ease-in-out',
			})
			animationData_8.opacity(0.1).translate3d(20, -50, 0).step({
					duration: 300
				})
				.opacity(1).translate3d(40, 240, 200).step({
					duration: 3000
				})
			that.setData({
				animationData_8: animationData_8.export()
			})
		}, 300)
		// 去下单
		setTimeout(function() {
			setInterval(function() {
				var animationData_9 = wx.createAnimation({
					duration: 3000,
					timingFunction: 'linear',
				})
				animationData_9.scale(1.0, 1.0).step({
						duration: 300
					})
					.scale(1.1, 1.1).step({
						duration: 300
					})
				that.setData({
					animationData_9: animationData_9.export()
				})
			}, 600)
		}, 1200)
	},
	//
	onTapTag: function(e) {
		var params = this.data.params,
			that = this;
		if (e.currentTarget.dataset.index == '1') {
			params.by = 'juli asc'
		}
		if (e.currentTarget.dataset.index == '2') {
			params.by = 'sales desc'
		}
		console.log(params, e.currentTarget.dataset.index)
		this.setData({
			sortSelected: that.data.sortList[0].sort,
			selected: e.currentTarget.dataset.index,
			params: params,
			pagenum: 1,
			storelist: [],
			bfstorelist: [],
			mygd: false,
			jzgd: true,
		});
		that.getstorelist();
	},
	sortSelected: function(e) {
		var params = this.data.params,
			that = this;
		if (e.currentTarget.dataset.sortindex == '0') {
			params.by = 'number asc'
		}
		if (e.currentTarget.dataset.sortindex == '1') {
			params.by = 'score desc'
		}
		if (e.currentTarget.dataset.sortindex == '2') {
			params.by = 'start_at asc'
		}
		if (e.currentTarget.dataset.sortindex == '3') {
			params.by = 'ps_money asc'
		}
		console.log(params, e.currentTarget.dataset.index, e.currentTarget.dataset.sortindex + 1)
		this.setData({
			selected: e.currentTarget.dataset.index,
			sortSelected: that.data.sortList[e.currentTarget.dataset.sortindex].sort,
			params: params,
			pagenum: 1,
			storelist: [],
			bfstorelist: [],
			mygd: false,
			jzgd: true,
		});
		that.getstorelist();
	},
	finish: function() {
		var params = this.data.params,
			that = this,
			info = this.data.characteristicSelected,
			characteristicList = this.data.characteristicList,
			discountSelected = this.data.discountSelected;
		this.setData({
			issx: true,
		})
		for (let i = 0; i < info.length; i++) {
			if (info[i]) {
				if (characteristicList[i].text == _('0配送费')) {
					params.nopsf = 1
				}
				if (characteristicList[i].text == _('0元起送')) {
					params.nostart = 1
				}
			}
		}
		if (discountSelected != null) {
			params.yhhd = this.data.discountList[discountSelected].zdname
		} else {
			params.yhhd = ''
		}
		that.setData({
			params: params,
			pagenum: 1,
			storelist: [],
			bfstorelist: [],
			mygd: false,
			jzgd: true,
		})
		that.getstorelist();
		console.log(params, this.data.issx, info, characteristicList, discountSelected)
	},
	clearSelectedNumb: function() {
		var params = this.data.params;
		params.nopsf = 2, params.nostart = 2, params.yhhd = ''
		this.setData({
			characteristicSelected: [false],
			discountSelected: null,
			selectedNumb: 0,
			issx: false,
			params: params,
			pagenum: 1,
			storelist: [],
			bfstorelist: [],
			mygd: false,
			jzgd: true,
		})
		this.getstorelist();
	},
	characteristicSelected: function(e) {
		var info = this.data.characteristicSelected;
		info[e.currentTarget.dataset.index] = !info[e.currentTarget.dataset.index];
		this.setData({
			characteristicSelected: info,
			selectedNumb: this.data.selectedNumb + (info[e.currentTarget.dataset.index] ? 1 : -1)
		})
		console.log(info, e.currentTarget.dataset.index, e.currentTarget.dataset.name);
	},
	discountSelected: function(e) {
		if (this.data.discountSelected != e.currentTarget.dataset.index) {
			this.setData({
				discountSelected: e.currentTarget.dataset.index,
				selectedNumb: this.data.selectedNumb + (this.data.discountSelected == null ? 1 : 0)
			})
		} else {
			this.setData({
				discountSelected: null,
				selectedNumb: this.data.selectedNumb - 1
			})
		}
	},
	mask1Cancel: function() {
		this.setData({
			mask1Hidden: true
		})
	},
	mask2Cancel: function() {
		this.setData({
			mask2Hidden: true
		})
	},
	onOverallTag: function(e) {
		console.log(e)
		this.setData({
			mask1Hidden: false
		})
	},
	onFilter: function() {
		this.setData({
			mask2Hidden: false
		})
	},
	hddb: function() {
		wx.pageScrollTo({
			scrollTop: 0,
			duration: 1000
		})
	},
	dwreLoad: function() {
		var that = this,
			params = this.data.params;
		wx.getLocation({
			type: 'wgs84',
			success: function(res) {
				let latitude = res.latitude
				let longitude = res.longitude
				let op = latitude + ',' + longitude;
				//console.log(op)
				params.lat = latitude, params.lng = longitude;
				that.setData({
					params: params,
				})
				that.getstorelist();
				// Brand
				a.util.request({
					'url': 'entry/wxapp/Brand',
					'cachetime': '0',
					data: {
						lat: latitude,
						lng: longitude
					},
					success: function(res) {
						//console.log(res.data)
						let _brand = res.data
						let _brand2Array = util.array2Two(_brand, 4);
						that.setData({
							Brand: res.data,
							brand2Array: _brand2Array,
						})
					},
				})
				// 调用接口
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: latitude,
						longitude: longitude
					},
					coord_type: 1,
					success: function(res) {
						var start = res.result.ad_info.location
						// console.log(res);
						// console.log(res.result.formatted_addresses.recommend);
						// console.log('坐标转地址后的经纬度：', res.result.ad_info.location)
						that.setData({
							weizhi: res.result.formatted_addresses
								.recommend,
							startjwd: start,
							// params: params,
						})
						// that.getstorelist();
						// // Brand
						// a.util.request({
						//   'url': 'entry/wxapp/Brand',
						//   'cachetime': '0',
						//   data: { lat: start.lat, lng: start.lng },
						//   success: function (res) {
						//     console.log(res.data)
						//     that.setData({
						//       Brand: res.data
						//     })
						//   },
						// })
					},
					fail: function(res) {
						console.log(res);
					},
					complete: function(res) {
						console.log(res);
					}
				});
			},
			fail: function() {
				wx.getSetting({
					success: (res) => {
						console.log(res)
						var authSetting = res.authSetting
						if (authSetting['scope.userLocation'] == false) {
							wx.showModal({
								title: _('提示'),
								content: _('您点击了拒绝授权,无法正常使用功能，点击确定重新获取授权。'),
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定')
										wx.openSetting({
											success: function success(
												res2) {
												if (res2
													.authSetting[
														"scope.userLocation"
														]) {
													that.dwreLoad()
												} else {
													that.dwreLoad()
												}
											}
										});
									}
								}
							})
						}
					}
				})
			},
			complete: function(res) {}
		})
	},
	getstorelist: function() {
		var that = this,
			page = that.data.pagenum;
		that.data.params.page = page, that.data.params.pagesize = 10;
		console.log(page, that.data.params);
		that.setData({
			isjzz: true
		})
		//推荐商家列表
		a.util.request({
			'url': 'entry/wxapp/StoreList',
			'cachetime': '0',
			data: that.data.params,
			success: function(res) {
				// console.log('分页返回的商家列表数据', res.data)
				if (res.data.length < 10) {
					that.setData({
						mygd: true,
						jzgd: true,
						isjzz: false
					})
				} else {
					that.setData({
						jzgd: true,
						pagenum: page + 1,
						isjzz: false
					})
				}
				for (let i = 0, l = res.data.length; i < l; i++) {
					if (res.data[i].sales == '0.0') {
						res.data[i].sales = '5.0'
					}
					var distance = parseFloat(res.data[i].juli)
					// console.log(distance)
					if (distance < 1000) {
						res.data[i].aa = distance + 'm'
						res.data[i].aa1 = distance
					} else {
						res.data[i].aa = (distance / 1000).toFixed(2) + 'km'
						res.data[i].aa1 = distance
					}
				}
				var storelist = that.data.bfstorelist;
				storelist = storelist.concat(res.data);
				if (that.data.mdxx.isyykg == 1) {
					let arr = [],
						arr2 = [];
					for (let i = 0, l = storelist.length; i < l; i++) {
						if (storelist[i].is_yy == 2) {
							arr2.push(storelist[i])
						} else {
							arr.push(storelist[i])
						}
					}
					storelist = arr.concat(arr2)
				}

				function unrepeat(arr) {
					var result = [];
					var obj = {};
					for (var i = 0; i < arr.length; i++) {
						if (!obj[arr[i].id]) {
							result.push(arr[i]);
							obj[arr[i].id] = true;
							console.log(obj)
						}
					}
					return result
				}
				storelist = unrepeat(storelist)
				that.setData({
					isxlsxz: false,
					storelist: storelist,
					bfstorelist: storelist,
				})
				//console.log(storelist)
			},
		})
	},
	jumps: function(e) {
		var that = this
		var id = e.currentTarget.dataset.id,
			name = e.currentTarget.dataset.name;
		var appid = e.currentTarget.dataset.appid
		var src = e.currentTarget.dataset.src,
			src2 = e.currentTarget.dataset.wb_src
		var type = e.currentTarget.dataset.type
		console.log(id, name, appid, src, src2, type)
		if (type == 1) {
			console.log(src)
			wx: wx.navigateTo({
				url: src,
			})
		} else if (type == 2) {
			wx.setStorageSync('vr', src2)
			wx: wx.navigateTo({
				url: '../car/car',
			})
		} else if (type == 3) {
			wx.navigateToMiniProgram({
				appId: appid,
			})
		}
	},
	tzsjxq: function(e) {
		console.log(e.currentTarget.dataset, this.data.mdxx)
		if (e.currentTarget.dataset.type == 1) {
			getApp().sjid = e.currentTarget.dataset.sjid
			wx.navigateTo({
				url: '/fs_wmall/pages/seller/index',
			})
		} else {
			if (this.data.mdxx.is_tzms == '1') {
				getApp().sjid = e.currentTarget.dataset.sjid
				wx.navigateTo({
					url: '/fs_wmall/pages/seller/index',
				})
			} else {
				wx.navigateTo({
					url: '/fs_wmall/pages/takeout/takeoutindex?storeid=' + e.currentTarget.dataset
						.sjid,
				})
			}
		}
	},
	qxd: function() {
		this.setData({
			istjhb: false,
		})
	},
	sssj: function() {
		wx.navigateTo({
			url: 'sssj',
		})
	},
	mSwitchCoupon() {
		let _start = this.data.mCouponShowStart
		let _end = this.data.mCouponShowEnd
		_start += 2
		_end += 2

		let _showData = this.data.mCouponList.slice(_start, _end)
		if (!_showData.length) {
			wx.showToast({
				title: _('没有更多的优惠券了'),
				icon: 'none',
				duration: 1500
			})
			return
		}
		this.setData({
			mCouponShowStart: _start,
			mCouponShowEnd: _end,
			mCouponShow: _showData
		})
	},
	mCouponFn(e) {
		const index = e.target.dataset.index
		this.setData({
			mCouponIndex: index,
			mCouponList: [],
			mCouponShowStart: 0,
			mCouponShowEnd: 2,
			mCouponShow: [],
		})
		this.mCoupon(index + 1)
	},
	mCoupon(typ = 1) {
		const _user_id = this.data.userinfo.id
		if (!_user_id) return
		a.util.request({
			'url': 'entry/wxapp/CouponsApi',
			data: {
				user_id: _user_id,
				type: typ
			},
			success: res => {
				let _data = res.data
				let _showData = _data.slice(this.data.mCouponShowStart, this.data.mCouponShowEnd)
				this.setData({
					mCouponList: _data,
					mCouponShow: _showData
				})
			}
		})
	},
	mScrollAd() {
		a.util.request({
			'url': 'entry/wxapp/scrollad',
			success: res => {
				this.setData({
					mScrollAdArr: res.data || []
				})
			}
		})
	},
	mYouxuan() {
		a.util.request({
			'url': 'entry/wxapp/RecommonGoodsApi',
			data: {},
			success: res => {
				let _data = res.data
				if (_data.length) {
					_data = _data.slice(0, 6)
					this.setData({
						mYouxuanList: _data
					})
				}
			}
		})
	},
	getCouponFn(e) {
		const _user_id = this.data.userinfo.id
		const _id = e.target.dataset.id
		const _state = e.target.dataset.state
		const _store_id = e.target.dataset.storeid

		if (_state === 1) {
			console.log("去下单")
			console.log(_store_id)
			getApp().sjid = _store_id
			wx.navigateTo({
				url: '/fs_wmall/pages/seller/index',
			})
			return
		}

		a.util.request({
			'url': 'entry/wxapp/LQCouponsApi',
			data: {
				user_id: _user_id,
				coupon_id: _id
			},
			success: res => {
				console.log(res)
				if (res.data.errcode === 0) {
					wx.showToast({
						title: _('领取成功'),
						icon: 'none',
						duration: 1500
					})
					this.mCoupon(this.data.mCouponIndex + 1)
				}
			}
		})

	},
	fangyiFn() {
		a.util.request({
			'url': 'entry/wxapp/fangyiApi',
			'cachetime': '0',
			success: res => {
				console.log(res)
				if (res.data.code === '1' && res.data.data) {
					let _data = res.data.data.country
					this.setData({
						fangyiData: _data
					})
				}
			}
		})
	},
	toDaySpecialDesc(e) {
		const {
			storeid
		} = e.currentTarget.dataset
		wx.navigateTo({
			url: '/fs_wmall/pages/takeout/takeoutindex?storeid=' + storeid
		})
	},
	daySpecialFn() {
		// 天天特价
		// tSaleWeek:week, // 天天特价星期
		// tPart:tPart, // 天天特价场次
		// tPagesize:6, // 天天特价6条数据
		const {
			tSaleWeek,
			tPart,
			tPagesize
		} = this.data
		a.util.request({
			'url': 'entry/wxapp/GetTttjApi',
			data: {
				weeks: tSaleWeek,
				days: tPart,
				pagesize: tPagesize
			},
			success: res => {
				let {
					data
				} = res
				let leftData = data.slice(0, 2) || []
				let rightData = data.slice(2, 6) || []
				console.log('t2yjy')
				console.log(data)
				console.log(leftData)
				console.log(rightData)
				this.setData({
					tDataLeft: leftData,
					tDataRight: rightData,
				})
			}
		})
	},
	// 蓝牙打印设置
	bluetoothSetting() {
		a.util.request({
			'url': 'entry/wxapp/bluetooth',
			success: res => {
				console.log('success')
				console.log(res)
				// 1:开启 2:关闭
				let isBluetooth = res.data == 1 ? true : false
				wx.setStorageSync('mBluetooth', isBluetooth)
			}
		})
	},
	// 切换语言
	switchLanguage: function() {
		base.switchLanguage.call(this)
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		const isWei = base.isWeiFn()
		this.setData({
			isWei
		})

		this.setData({
			weekArr: [_('星期一'), _('星期二'), _('星期三'), _('星期四'), _('星期五'), _('星期六'), _('星期日')],
			mCouponTitleArr: [_('外卖券'), _('堂食券'), _('通用券')],
			sortSelected: _("综合排序"),
			characteristicList: [{
				text: _('0配送费')
			}, {
				text: _("0元起送")
			}],
			sortList: [{
				sort: _("综合排序"),
				image: "",
			}, {
				sort: _("销量最高"),
				image: "",
			}, {
				sort: _("起送价最低"),
				image: "",
			}, {
				sort: _("配送费最低"),
				image: "",
			}],
			discountList: [{
				icon: _("减"),
				iconColor: "#FF635B",
				text: _("满减优惠"),
				zdname: ' and d.money is not null'
			}, {
				icon: _("新"),
				iconColor: "#34aaff",
				text: _("新用户立减"),
				zdname: ' and c.xyh_open=1'
			}, {
				icon: _("提"),
				iconColor: "#6FDF64",
				text: _("到店自提"),
				zdname: ' and c.is_zt=1'
			}],
		})

		if (base.getLanguage() === 'zh_CN') {
			this.setData({
				language: '中文',
			})
		} else {
			this.setData({
				language: 'wei',
			})
		};

		this.bluetoothSetting()

		let timer = setInterval(() => {
			let tCountDown = countDown()
			this.setData({
				tCountDown
			})
		}, 1000)
		this.setData({
			tTimer: timer
		})
		var that = this;
		that.setData({
			isxlsxz: true
		})
		this.fangyiFn();
		this.daySpecialFn();
		// a.setNavigationBarColor(this);
		a.pageOnLoad(this);
		a.getUserInfo(function(userinfo) {
			a.util.request({
				'url': 'entry/wxapp/CouponSet',
				'cachetime': '0',
				success: function(res) {
					that.setData({
						userinfo,
						CouponSet: res.data,
					})
					// mf - 首页优惠券
					that.mCoupon()
					var current_time = util.formatTime(new Date()).slice(11, 16)
					console.log(res.data, current_time)
					if (current_time >= res.data.time && current_time < res.data
						.time2) {
						console.log('hbtime')
						if (res.data.is_tjhb == '1') {
							a.util.request({
								'url': 'entry/wxapp/TjCoupons',
								'cachetime': '0',
								data: {
									user_id: userinfo.id
								},
								success: function(res) {
									console.log(res.data, typeof(res.data))
									if (res.data == _('暂无红包')) {

									}
									if (res.data == _('今日已领')) {

									}
									if (typeof(res.data) == "object") {
										console.log(typeof(res.data))
										that.setData({
											istjhb: true,
											tjhbarr: res.data,
										})
										that.refresh()
									}
								},
							})
						}
					} else {
						console.log('nothbtime')
					}
				},
			})
		})
		// 系统设置
		a.util.request({
			'url': 'entry/wxapp/system',
			'cachetime': '0',
			success: (res) => {
				that.setData({
					colorOne: res.data.color,
					colorTwo: res.data.colortwo || res.data.color,
					mLogo: res.data.logo,
					mName: res.data.link_name,
					lang_status: res.data.lang_status,
				})

				var xtxx = res.data;
				// 实例化API核心类
				qqmapsdk = new QQMapWX({
					key: res.data.map_key
				});
				wx.setNavigationBarTitle({
					title: res.data.url_name,
				})
				that.setData({
					mdxx: res.data
				})
				//home分类
				a.util.request({
					'url': 'entry/wxapp/TypeAd',
					'cachetime': '0',
					success: function(res) {
						console.log(res.data)
						var navs = [];
						if (xtxx.fl_more == '1') {
							for (var i = 0, len = res.data.length; i < len; i +=
								8) {
								navs.push(res.data.slice(i, i + 8));
							}
						}
						if (xtxx.fl_more == '2') {
							for (var i = 0, len = res.data.length; i < len; i +=
								10) {
								navs.push(res.data.slice(i, i + 10));
							}
						}
						console.log(navs)
						that.setData({
							navs: navs,
						})
					},
				})
				that.dwreLoad()
				wx.setStorageSync('bqxx', res.data)
			},
		})

		//llz
		a.util.request({
			'url': 'entry/wxapp/Llz',
			'cachetime': '0',
			data: {
				type: '1,2'
			},
			success: function(res) {
				console.log(res)
				var dbllz = [],
					zbllz = [];
				for (let i = 0; i < res.data.length; i++) {
					if (res.data[i].type == 1) {
						dbllz.push(res.data[i])
					}
					if (res.data[i].type == 2) {
						zbllz.push(res.data[i])
					}
				}
				that.setData({
					dbllz: dbllz,
					zbllz: zbllz
				})
			},
		})
		// ZbOrder
		a.util.request({
			'url': 'entry/wxapp/ZbOrder',
			'cachetime': '0',
			success: function(res) {
				console.log(res.data)
				that.setData({
					ZbOrder: res.data
				})
			},
		})
		a.util.request({
			'url': 'entry/wxapp/QgGoods',
			'cachetime': '0',
			data: {
				type_id: '',
				store_id: '',
				page: 1,
				pagesize: 10,
				type: 1
			},
			success: function(res) {
				console.log('分页返回的列表数据', res.data)
				for (let i = 0; i < res.data.length; i++) {
					res.data[i].discount = (Number(res.data[i].money) / Number(res.data[i]
						.price) * 10).toFixed(1)
					res.data[i].yqnum = (((Number(res.data[i].number) - Number(res.data[i]
						.surplus)) / Number(res.data[i].number) * 100)).toFixed(1)
				}
				that.setData({
					qglist: res.data,
				})
			}
		});
		//home轮播图和开屏公告
		a.util.request({
			'url': 'entry/wxapp/ad',
			'cachetime': '0',
			success: function(res) {
				console.log(res)
				var toplb = [],
					zblb = [],
					dblb = [],
					zjbk = [];
				for (var i = 0; i < res.data.length; i++) {
					if (res.data[i].type == '1') {
						toplb.push(res.data[i])
					} else if (res.data[i].type == '3') {
						zblb.push(res.data[i])
					} else if (res.data[i].type == '4') {
						dblb.push(res.data[i])
					} else if (res.data[i].type == '11') {
						zjbk.push(res.data[i])
					}
				}
				//console.log(toplb, zblb, dblb,zjbk)
				that.setData({
					toplb,
					zblb,
					dblb,
					cardlist: {
						upDownPadding: 10,
						leftRightPadding: 15,
						numberOfRow: 2,
						pictureList: zjbk,
						radius: 10,
						padding: 10,
						height: 190,
					},
				})
			},
		})
		// mf -- 为您优选
		this.mYouxuan()
		// mf --首页滚动广告
		this.mScrollAd()
	},
	onPageScroll: function(e) {
		// console.log(e)
		if (e.scrollTop > 0) {
			this.setData({
				topmove: true
			})
		} else {
			this.setData({
				topmove: false
			})
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.setData({
			_t: base._t()
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
		this.setData({
			tTimer: null
		})
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		this.setData({
			tTimer: null
		})
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		var that = this
		this.setData({
			issx: false,
			selected: 0,
			mask1Hidden: true,
			mask2Hidden: true,
			animationData: "",
			location: "",
			characteristicSelected: [false, false],
			discountSelected: null,
			selectedNumb: 0,
			sortSelected: _("综合排序"),
			params: {
				nopsf: 2,
				nostart: 2,
				yhhd: ''
			},
			pagenum: 1,
			storelist: [],
			bfstorelist: [],
			mygd: false,
			jzgd: true,
		})
		console.log('下拉刷新', this.data.pagenum, this.data.isxlsxz)
		if (!this.data.isxlsxz) {
			this.setData({
				jzgd: false
			})
			this.onLoad();
		}
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		console.log('上拉加载', this.data.pagenum)
		var that = this;
		if (!this.data.mygd && this.data.jzgd && !this.data.isjzz) {
			this.setData({
				jzgd: false
			})
			this.getstorelist();
		} else {
			// wx.showToast({
			//   title: '没有更多了',
			//   icon: 'loading',
			//   duration: 1000,
			// })
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		var that = this,
			fx_title = (that.data.mdxx.fx_title == '' ? that.data.mdxx.url_name : that.data.mdxx.fx_title);
		return {
			title: fx_title,
			path: '/fs_wmall/pages/Liar/loginindex',
			success: function(res) {
				// 转发成功
			},
			fail: function(res) {
				// 转发失败
			}
		}
	},
	onShareTimeline(e) {
		// console.log("点击分享pyq", e)
		return {
			title: fx_title,
			path: '/fs_wmall/pages/Liar/loginindex',
		}
	}
})
